package com.meichis.yndms.repository.manager;

import android.util.ArrayMap;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.reflect.TypeToken;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.yndms.common.DataCenter;
import com.meichis.yndms.http.CallSubscriber;
import com.meichis.yndms.http.IWebServiceCallback;
import com.meichis.yndms.http.RetrofitUtil.RxSchedulersHelper;
import com.meichis.yndms.model.DicDataItem;
import com.meichis.yndms.model.VisitTemplate;
import com.meichis.yndms.repository.ApiUtil;
import com.meichis.yndms.room.AppDatabase;

import java.util.List;

/**
 * @author xmy
 * @title
 * @remake
 * @create 2021-04-14 22:51
 */
public class SyncBaseDataManager extends BaseManager {

    private static SyncBaseDataManager INSTANCE;

    public static SyncBaseDataManager getInstance() {
        if (INSTANCE == null) {
            synchronized (SyncBaseDataManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new SyncBaseDataManager();
                }
            }
        }
        return INSTANCE;
    }

    public void GetVisitTemplateList(final MutableLiveData<Boolean> showDialog, final MutableLiveData<Boolean> isResult) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("TDP", 1002);
        getRootApi().RequestSFA(getToken(), ApiUtil.getRequestPack(hs, "PBMService.GetVisitTemplateList"))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<VisitTemplate>>() {
                }.getType(), new IWebServiceCallback<List<VisitTemplate>>() {
                    @Override
                    public void onSucceed(int sequence, List<VisitTemplate> resultPack, String ReturnInfo, int Return) {
                        AppDatabase.getInstance().visitTemplateDao().deleteAll();
                        if (resultPack != null && resultPack.size() >= 0) {
                            MessageTools.showLongToast("同步成功！");
                            for (VisitTemplate template : resultPack) {
                                template.setOwnerClient(DataCenter.Manufacturer);
                            }
                            AppDatabase.getInstance().visitTemplateDao().insert(resultPack);
                            isResult.setValue(true);
                        } else {
                            MessageTools.showLongToast("暂无数据！");
                        }
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        isResult.setValue(false);
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));
    }

    public void GetDicByTableNamesJson(final MutableLiveData<Boolean> showDialog, final MutableLiveData<Boolean> isResult) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("TDP", DataCenter.Manufacturer);
        getRootApi().RequestSFA(getToken(), ApiUtil.getRequestPack(hs, "PublicService.GetDicByTableNamesJson"))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<DicDataItem>>() {
                }.getType(), new IWebServiceCallback<List<DicDataItem>>() {
                    @Override
                    public void onSucceed(int sequence, List<DicDataItem> resultPack, String ReturnInfo, int Return) {

                        if (resultPack != null && resultPack.size() >= 0) {
                            MessageTools.showLongToast("同步成功！");
                            AppDatabase.getInstance().dicDataItemDao().insertAll(resultPack);

                        } else {
                            MessageTools.showLongToast("暂无数据！");
                        }
                        isResult.setValue(true);
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        isResult.setValue(false);
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));
    }

}
